<template>
  <TemplateTable ref="tableRef" :data="tableData" :columns="tableColumns" :loading="loading"
    :height="470" :page="pageIndex" :page-size="pageSize" :total="total" border="none" size="small"
    cell-class-name="table-cell" highlight-current-row stripe @page-change="handlePageChange"
    @cell-click="handleCellClick">
  </TemplateTable>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import moment from 'moment'

import { metadata } from '@/api/modules/modelSetting'

const emit = defineEmits(['selected'])

const loading = ref(false)
const total = ref(0)
const pageIndex = ref(1)
const pageSize = 20
const tableData = ref([])

const tableColumns = computed(() => [
  { title: '中文名', field: 'name', minWidth: 200 },
  { title: '表名', field: 'tablename', minWidth: 200 },
  { title: '代码', field: 'code', minWidth: 140 },
  // { title: '类型', field: 'tabletype', minWidth: 120, formatter: ({ row }) => {
  //   const rowData = TAB_OPTIONS.find(item => item.value === row.tabletype)
  //   return rowData?.label
  // } },
  { title: 'ES数据', field: 'fromdb', slot: 'fromdb', minWidth: 140 },
  {
    title: '创建时间',
    field: 'createtime',
    minWidth: 140,
    formatter: ({ row }) => {
      return row.createtime ? moment(row.createtime).format('YYYY-MM-DD HH:mm:ss') : ''
    }
  },
  { title: '备注', field: 'eventname', minWidth: 140 }
])

// 获取数据源列表
const getPageData = () => {
  loading.value = true

  metadata
    .page({
      query: {
        p: pageIndex.value,
        ps: pageSize
      }
    })
    .then(({ data }) => {
      tableData.value = data?.content || []
      total.value = data?.totalElements || 0
    })
    .finally(() => {
      loading.value = false
    })
}

const handleCellClick = ({ row }) => {
  emit('selected', {
    dbid: row.dbid,
    id: row.id
  })
}

// 处理页码变化
const handlePageChange = page => {
  pageIndex.value = page
  getPageData()
}

onMounted(() => {
  handlePageChange()
})
</script>

<style scoped lang="scss">
:deep(.table-cell) {
  cursor: pointer;
}
</style>